package ruixinyi.experiment12.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ruixinyi.experiment12.model.Student;
import ruixinyi.experiment12.service.StudentService;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 获取所有学生信息
    @GetMapping("/getAll")
    public List<Student> getAllStudents() {
        return studentService.findAllStudents();
    }

    // 创建新的学生信息
    @PostMapping("/add")
    public Student createStudent(@RequestBody Student student) {
        System.out.println(student);
        return studentService.createStudent(student);
    }

    // 通过ID获取学生信息
    @GetMapping("/get/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable Integer id) {
        Student student = studentService.findStudentById(id);
        return ResponseEntity.ok(student);
    }

    // 更新学生信息
    @PutMapping("/put/{id}")
    public ResponseEntity<Student> updateStudent(@PathVariable Integer id, @RequestBody Student studentDetails) {
        Student updatedStudent = studentService.updateStudent(id, studentDetails);
        return ResponseEntity.ok(updatedStudent);
    }

    // 删除学生信息
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable Integer id) {
        studentService.deleteStudent(id);
        return ResponseEntity.noContent().build();
    }
}
